package com.itbuka.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itbuka.goods.domain.GoodsProduct;
import com.itbuka.goods.mapper.GoodsProductMapper;
import com.itbuka.goods.service.GoodsProductService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;


import java.util.List;

/**
 * @author ld
 * @description 针对表【goods_product(商品表)】的数据库操作Service实现
 * @createDate 2024-07-30 11:45:59
 */
@Service
public class GoodsProductServiceImpl implements GoodsProductService {
    @Autowired
    private GoodsProductMapper goodsProductMapper;

    @Override
    public List<GoodsProduct> selectAll() {
        LambdaQueryWrapper<GoodsProduct> lqw = new LambdaQueryWrapper<>();
        lqw.eq(GoodsProduct::getIsDelete, 0);
        return goodsProductMapper.selectList(lqw);
    }

    @Override
    public List<GoodsProduct> selectList(GoodsProduct goodsProduct) {
        LambdaQueryWrapper<GoodsProduct> lqw = this.lqw(goodsProduct);
        return goodsProductMapper.selectList(lqw);
    }

    @Override
    public Integer insert(GoodsProduct goodsProduct) {
        return goodsProductMapper.insert(goodsProduct);
    }

    @Override
    @Transactional
    public Integer delete(String ids) {
        try {
            String[] split = ids.split(",");
            for (String id : split) {
                GoodsProduct goodsProduct = new GoodsProduct();
                goodsProduct.setId(Long.parseLong(id));
                goodsProduct.setIsDelete(1);
                goodsProductMapper.updateById(goodsProduct);
            }
            return 1;
        } catch (Exception e) {
            return -1;
        }
    }

    @Override
    public Integer update(GoodsProduct goodsProduct) {
        return goodsProductMapper.updateById(goodsProduct);
    }

    @Override
    public Page<GoodsProduct> pageAll(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        return (Page<GoodsProduct>) this.selectAll();
    }

    @Override
    public Page<GoodsProduct> pageList(GoodsProduct goodsProduct, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        return (Page<GoodsProduct>) this.selectList(goodsProduct);
    }

    @Override
    public Integer audit(Long id, Integer audit) {
        GoodsProduct product = new GoodsProduct();
        product.setId(id);
        GoodsProduct product1 = goodsProductMapper.selectById(id);
        if (product1.getIsDelete() == 0) {
            if (audit == 2) {
                //审核拒绝
                product.setAudit(audit);
                goodsProductMapper.updateById(product);
                return 2;
            }else if (audit == 1){
                //审核通过
                product.setAudit(audit);
                product.setStatus(1);
                goodsProductMapper.updateById(product);
                return 1;
            }
        }
        return -1;
    }

    @Override
    public Integer status(Long id, Integer status) {
        GoodsProduct goodsProduct = goodsProductMapper.selectById(id);
        if (goodsProduct == null || goodsProduct.getIsDelete() == 0) {
            return -1;
        }
        //上架
        if (status == 1) {
            Integer audit = goodsProduct.getAudit();
            if (audit != 1) {
                return -1;
            }
        }
        GoodsProduct product = new GoodsProduct();
        product.setId(id);
        product.setStatus(status);
        return goodsProductMapper.updateById(product);
    }

    public LambdaQueryWrapper<GoodsProduct> lqw(GoodsProduct goodsProduct) {
        LambdaQueryWrapper<GoodsProduct> lqw = new LambdaQueryWrapper<>();
        if (goodsProduct.getId() != null) {
            lqw.eq(GoodsProduct::getId, goodsProduct.getId());
        }
        if (goodsProduct.getName() != null) {
            lqw.eq(GoodsProduct::getName, goodsProduct.getName());
        }
        if (goodsProduct.getPrice() != null) {
            lqw.eq(GoodsProduct::getPrice, goodsProduct.getPrice());
        }
        if (goodsProduct.getSalesModel() != null) {
            lqw.eq(GoodsProduct::getSalesModel, goodsProduct.getSalesModel());
        }
        if (goodsProduct.getType() != null) {
            lqw.eq(GoodsProduct::getType, goodsProduct.getType());
        }
        if (goodsProduct.getStatus() != null) {
            lqw.eq(GoodsProduct::getStatus, goodsProduct.getStatus());
        }
        if (goodsProduct.getAudit() != null) {
            lqw.eq(GoodsProduct::getAudit, goodsProduct.getAudit());
        }
        if (goodsProduct.getShopName() != null) {
            lqw.eq(GoodsProduct::getShopName, goodsProduct.getShopName());
        }
        if (goodsProduct.getClassifyId() != null) {
            lqw.eq(GoodsProduct::getClassifyId, goodsProduct.getClassifyId());
        }
        if (goodsProduct.getSelling() != null) {
            lqw.eq(GoodsProduct::getSelling, goodsProduct.getSelling());
        }
        if (goodsProduct.getParameter() != null) {
            lqw.eq(GoodsProduct::getParameter, goodsProduct.getParameter());
        }
        if (goodsProduct.getUnitMeasure() != null) {
            lqw.eq(GoodsProduct::getUnitMeasure, goodsProduct.getUnitMeasure());
        }
        if (goodsProduct.getImg() != null) {
            lqw.eq(GoodsProduct::getImg, goodsProduct.getImg());
        }
        if (goodsProduct.getVideo() != null) {
            lqw.eq(GoodsProduct::getVideo, goodsProduct.getVideo());
        }
        if (goodsProduct.getDescription() != null) {
            lqw.eq(GoodsProduct::getDescription, goodsProduct.getDescription());
        }
        if (goodsProduct.getIsDelete() != null) {
            lqw.eq(GoodsProduct::getIsDelete, goodsProduct.getIsDelete());
        }
        if (goodsProduct.getCreateTime() != null) {
            lqw.eq(GoodsProduct::getCreateTime, goodsProduct.getCreateTime());
        }
        if (goodsProduct.getUpdateTime() != null) {
            lqw.eq(GoodsProduct::getUpdateTime, goodsProduct.getUpdateTime());
        }
        if (goodsProduct.getBrandId() != null) {
            lqw.eq(GoodsProduct::getBrandId, goodsProduct.getBrandId());
        }
        return lqw;
    }
}




